package com.nervenets.general.web.listener;

import com.nervenets.general.Global;
import com.nervenets.general.web.vo.FileUploadProgress;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

@Component
public class UploadProgressListener implements ProgressListener {
    private HttpSession session;

    @Override
    public void update(long readBytes, long allBytes, int index) {
        FileUploadProgress userEntity = (FileUploadProgress) session.getAttribute(Global.Constants.FILE_UPLOAD_PROGRESS_KEY);
        userEntity.setReadBytes(readBytes);
        userEntity.setAllBytes(allBytes);
        userEntity.setCurrentIndex(index);
    }

    public void setSession(HttpSession session) {
        this.session = session;
        session.setAttribute(Global.Constants.FILE_UPLOAD_PROGRESS_KEY, new FileUploadProgress());
    }
}
